package com.huluobo.apps

import com.huluobo.lib_base.BaseViewModel
import com.huluobo.lib_base.Res
import com.huluobo.lib_base.UIIntent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

/**
 *  Created by LC on 2025/6/23.
 *  @HiltViewModel 标记是一个Hilt的ViewModel,可以直接通过hilt创建实例
 *   @Inject constructor 对外自动提供他的构造方法,
 */
@HiltViewModel
class AppsViewModel @Inject constructor(private val appsRepo: AppsRepo) : BaseViewModel<AppsIntent>() {
    override fun handleIntent(it: UIIntent) {
        when (it) {
            is AppsIntent.GetApps -> {
                //由于httpRequest方法的返回值是固定的,所以需要在这里进行强转,然后在获取对应成功状态的位置,强转回来
                httpRequest(appsRepo.getApps() as Flow<Res<Any>>)
            }

            is AppsIntent.DownloadApk -> {
                download(it.url, it.file)
            }
        }
    }
}